﻿#include "data_receive.h"
#include <sstream>

int receive_data (string file_path, vector<vector<double>>& data_list)
{
	vector<string> str_data_list; // 문자열 데이터 리스트
	
	ifstream usr_input(file_path);

	if(!usr_input.is_open())
	{
		cout << "FILE OPENING ERROR!" << endl;
	}

	// 파일에서 데이터를 문자열 형태로 읽어온다.
	string temp_data;
	char string_data_size[1000];
	while (usr_input.good())
	{
		usr_input.getline(string_data_size, 1000);
		temp_data = string_data_size;
		str_data_list.push_back(temp_data);
	}

	set_array_from_string(data_list, str_data_list);
	cout << "입력된 데이터 행 수 : " << data_list.size() << endl;
	cout << "입력된 데이터 열 수 : " << data_list[0].size() << endl;

	return 0;
}
void set_array_from_string(vector<vector<double> >& data_list, vector<string>& str_data_list)
{
	int col_size = 0;
	int col = 0, row = 0;
	stringstream str_buf;

	vector<string>::iterator itr_str = str_data_list.begin();
	while (itr_str != str_data_list.end())
	{
		str_buf.clear();
		str_buf << *itr_str;
		
		if(row == 0)
		{
			int data;
			vector<double> temp_list;
			for(int i = 0; str_buf.good(); i++)
			{
				str_buf >> data;
				temp_list.push_back(data);
				col_size ++;
			}
			data_list.push_back(temp_list);
		}
		else
		{
			int data;
			vector<double> temp_list;
			for(int i = 0; i < col_size; i++)
			{
				str_buf >> data;
				temp_list.push_back(data);
			}
			data_list.push_back(temp_list);
		}
		row ++;
		itr_str ++;
	}

}